package com.teach.web;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.teach.Const;
import com.teach.dto.LoginUser;
import com.teach.dto.UserRegDTO;
import com.teach.service.UserService;

@Controller
@RequestMapping("/")
public class UserController {
	
	private static final Logger log = LoggerFactory.getLogger(UserController.class);
	
	@Autowired
	private UserService userService;

	@PostMapping("login-1"+Const.WEB_EXT)
	public String doLogin(String userName,String userPassword,HttpSession session,Model model) {
		try {
			LoginUser user = userService.login(userName, userPassword);
			session.setAttribute(Const.SESSION_LOGIN_USER, user);
			model.addAttribute("msg", "登录成功");
			model.addAttribute("text", "进入个人中心");
			model.addAttribute("href", "my/");
		}catch (Exception e) {
			log.debug("登录失败",e);
			model.addAttribute("error",e.getMessage());
		}
		return "msg";
	}
	
	@PostMapping("reg"+Const.WEB_EXT)
	public String doReg(UserRegDTO user,Model model) {
		try {
			userService.reg(user);
			model.addAttribute("msg", "注册成功");
			model.addAttribute("text", "返回首页");
			model.addAttribute("href", "./");
		}catch (Exception e) {
			log.debug("注册失败",e);
			model.addAttribute("error",e.getMessage());
		}
		return "msg";
	}
}
